package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;

public class OfficeClient extends Client {
	ArrayList<Office> offices = null;
	//private Service service;

	public OfficeClient(Service s) {
		super(s);
		//service = s;
	}

	public ArrayList<Office> getOfficeList() {
		try {
			Service t = getServer();
			Socket socket = new Socket(t.getIpAddress(), t.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("getOfficeList,");
			out.flush();
			BufferedReader in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			String input = in.readLine();
			Scanner inputScanner = new Scanner(input);
			inputScanner.useDelimiter(",");
			String returnName = inputScanner.next();
			int noOffices = inputScanner.nextInt();
			offices = new ArrayList<Office>();
			for (int i = 0; i < noOffices; i++) {
				Office thisOffice = new Office(inputScanner.nextInt(),
						inputScanner.next(), inputScanner.next(), inputScanner
								.next(), inputScanner.next());
				boolean dos = false;
				for (Office o : offices) {
					if (thisOffice.getRef() == o.getRef()) {
						dos = true;
					}
				}
				if (!dos) {
					offices.add(thisOffice);
				}
			}
			out.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("returning");
		return offices;
	}

	public Vector<String> getOfficeInfo(int officeRef) {
		Office thisOffice = null;
		for (Office o : offices) {
			if (o.getRef() == officeRef)
				thisOffice = o;
		}
		try {
			Service t = getServer();
			Socket socket = new Socket(t.getIpAddress(), t.getPort());
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("getOfficeInfo," + officeRef + ",");
			out.flush();
			BufferedReader in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			String input = in.readLine();
			Scanner inputScanner = new Scanner(input);
			inputScanner.useDelimiter(",");
			String returnName = inputScanner.next();
			inputScanner.next();
			int noServices = inputScanner.nextInt();
			for (int i = 0; i < noServices; i++) {
				String ser = inputScanner.next();
				boolean there = false;
				for (String serv : thisOffice.getOffServices()) {
					if (ser.equals(serv))
						there = true;
				}
				if (!there) {
					thisOffice.addOfficeService(ser);
				}
			}
			out.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return thisOffice.getOffServices();
	}

	public ArrayList<Office> searchServices(String service) {
		ArrayList<Office> of = new ArrayList<Office>();
		if (offices == null) {
			getOfficeList();
		}
		for (Office office : offices) {
			if (office.getOffServices().size() == 0) {
				getOfficeInfo(office.getRef());
			}
			for (String services : office.getOffServices()) {
				if (services.contains(service) && !of.contains(office)) {
					of.add(office);
				}
			}
		}
		return of;
	}
}
